home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
c
/
c_eval.zip
/
EE.C
next >
Wrap
Text File
|
1993-04-23
|
27KB
|
769 lines
/*************************************************************************
** **
** EE.C Expression Evaluator **
** **
** AUTHOR: Mark Morley **
** COPYRIGHT: (c) 1992 by Mark Morley **
** DATE: December 1991 **
** HISTORY: Jan 1992 - Made it squash all command line arguments **
** into one big long string. **
** - It now can set/get VMS symbols as if they **
** were variables. **
** - Changed max variable name length from 5 to 15 **
** Jun 1992 - Updated comments and docs **
** **
** You are free to incorporate this code into your own works, even if it **
** is a commercial application. However, you may not charge anyone else **
** for the use of this code! If you intend to distribute your code, **
** I'd appreciate it if you left this message intact. I'd like to **
** receive credit wherever it is appropriate. Thanks! **
** **
** I don't promise that this code does what you think it does... **
** **
** Please mail any bug reports/fixes/enhancments to me at: **
** morley@camosun.bc.ca **
** or **
** Mark Morley **
** 3889 Mildred Street **
** Victoria, BC Canada **
** V8Z 7G1 **
** (604) 479-7861 **
** **
*************************************************************************/
/* #define VAX */ /* Uncomment this line if you're using VMS */
#include <stdlib.h>
#include <math.h>
#include <setjmp.h>
#ifdef VAX
#include <ssdef.h>
#include <descrip.h>
#endif
#include "ee.h"
#define ERR(n) {ERROR=n; ERPOS=expression-ERANC-1; strcpy(ERTOK,token); longjmp(jb,1);}
/* These defines only happen if the values are not already defined! You may
want to add more precision here, if your machine supports it. */
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#ifndef M_E
#define M_E 2.71828182845904523536
#endif
/*************************************************************************
** **
** PROTOTYPES FOR CUSTOM MATH FUNCTIONS **
** **
*************************************************************************/
double deg( double x );
double rad( double x );
/*************************************************************************
** **
** VARIABLE DECLARATIONS **
** **
*************************************************************************/
int ERROR; /* The error number */
char ERTOK[TOKLEN + 1]; /* The token that generated the error */
int ERPOS; /* The offset from the start of the expression */
char* ERANC; /* Used to calculate ERPOS */
/*
Add any "constants" here... These are "read-only" values that are
provided as a convienence to the user. Their values can not be
permanently changed. The first field is the variable name, the second
is its value.
*/
VARIABLE Consts[] =
{
/* name, value */
{ "pi", M_PI },
{ "e", M_E },
{ 0 }
};
/*
Add any math functions that you wish to recognise here... The first
field is the name of the function as it would appear in an expression.
The second field tells how many arguments to expect. The third is
a pointer to the actual function to use.
*/
FUNCTION Funcs[] =
{
/* name, funtion to call */
{ "sin", 1, sin },
{ "cos", 1, cos },
{ "tan", 1, tan },
{ "asin", 1, asin },
{ "acos", 1, acos },
{ "atan", 1, atan },
{ "sinh", 1, sinh },
{ "cosh", 1, cosh },
{ "tanh", 1, tanh },
{ "exp", 1, exp },
{ "log", 1, log },
{ "log10", 1, log10 },
{ "sqrt", 1, sqrt },
{ "floor", 1, floor },
{ "ceil", 1, ceil },
{ "abs", 1, fabs },
{ "hypot", 2, hypot },
{ "deg", 1, deg },
{ "rad", 1, rad },
{ 0 }
};
VARIABLE Vars[MAXVARS]; /* Array for user-defined variables */
unsigned char* expression; /* Pointer to the user's expression */
unsigned char token[TOKLEN + 1]; /* Holds the current token */
int type; /* Type of the current token */
jmp_buf jb; /* jmp_buf for errors */
/*************************************************************************
** **
** Some custom math functions... Note that they must be prototyped **
** above (if your compiler requires it) **
** **
** deg( x ) Converts x radians to degrees. **
** rad( x ) Converts x degrees to radians. **
** **
*************************************************************************/
double
deg( double x )
{
return( x * 180.0 / M_PI );
}
double
rad( double x )
{
return( x * M_PI / 180.0 );
}
/*************************************************************************
** **
** GetSymbol( char* s ) **
** **
** This routine obtains a value from the program's environment. **
** This works for DOS and VMS (and other OS's???)
** **
************************************************************************/
GetSymbol( char* s, TYPE* v )
{
char* e;
e = getenv( s );
if( !e )
return( 0 );
*v = atof( e );
return( 1 );
}
/*************************************************************************
** **
** SetSymbol( char* s, char* v ) **
** **
** This VMS specific routine sets (or updates) a VMS symbol to a given **
** value **
** **
*************************************************************************/
#ifdef VAX
SetSymbol( char* s, char* v )
{
struct dsc$descriptor_s sym;
struct dsc$descriptor_s val;
long typ = 1;
sym.dsc$w_length = strlen( s );
sym.dsc$a_pointer = s;
sym.